package com.krazevina.kar;

import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.SelectionListener;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.plaf.Border;
import com.sun.lwuit.plaf.Style;
import java.io.IOException;
import javax.microedition.rms.RecordStore;

public final class AddressBook extends Form{
    List lv;
    Button bcall, bookmark, addressbook, setting;
    Image fav,nofav;
    Dialog menu;
    Button title;
    static boolean headerSearch = false;
    static Image backgr;
    TaxiResource taxr;
    int idProv = 24;
    String province[]=new String[]{
"An Giang","Bắc Cạn","Bắc Giang","Bạc Liêu","Bắc Ninh","Bến Tre","Bình Định","Bình Dương",
"Bình Phước","Bình Thuận","Cà Mau","Cam Ranh","Cần Thơ","Cao Bằng","Đà Lạt","Đà Nẵng",
"Đăk Lăk","Điện Biên","Đồng Nai","Đồng Tháp","Gia Lai","HCM","Hà Giang","Hà Nam","Hà Nội",
"Hà Tĩnh","Hải Dương","Hải Phòng","Hậu Giang","Hòa Bình","Hội An","Huế","Hưng Yên","Kiên Giang",
"Kon Tum","Lai Châu","Lâm Đồng","Lạng Sơn","Lào Cai","Long An","Nghệ An","Nha Trang","Ninh Bình",
"Ninh Thuận","Phan Rang","Phú Thọ","Phú Yên","Quảng Bình","Quảng Ngãi","Quảng Ninh","Quảng Trị",
"Sóc Trăng","Sơn La","Tam Kỳ","Tây Ninh","Thái Nguyên","Thanh Hoá","Tiền Giang","Trà Vinh","Tuy Hòa",
"Tuyên Quang","Vĩnh Long","Vĩnh Phúc","Vũng Tàu","Yên Bái"
    };
    int lastIndex=-1;
    Taxi currTaxi = null;
    public AddressBook() {
        taxr = MainForm.taxr;
        getStyle().setBgTransparency(255);
        lv = new List();
        lv.setPreferredH(getHeight()-89);
            lv.setPreferredW(getWidth());
        try{
            byte[]buffer = new byte[4];
            RecordStore rs = RecordStore.openRecordStore("currentProvince",true);
            rs.getRecord(rs.getNextRecordID()-1, buffer, 0);
            idProv = Int(buffer);
        }catch(Exception e){}
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        addView();
        addComponent(lv);
        addTaxi();
    }

    void addView(){
        Container topcon = new Container(new BoxLayout(BoxLayout.X_AXIS));
        topcon.setPreferredW(getWidth());
        topcon.setPreferredH(40);
        title = new Button(((Prov)taxr.prov.elementAt(idProv)).name.substring(5));
        title.setPreferredW(getWidth()-104);
        title.setPreferredH(40);
        Style s = new Style();
        s.setAlignment(Container.LEFT);
        s.setBorder(null);s.setBgTransparency(0);
        s.setMargin(0,0,0,0);s.setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
        s.setPadding(0,0,10,0);
        s.setFgColor(0xffffff);
        title.setUnselectedStyle(s);
        title.setSelectedStyle(new Style(s));
        title.setPressedStyle(new Style(s));
        title.getSelectedStyle().setFgColor(0x4898d0);
        title.getPressedStyle().setFgColor(0x4898d0);
        title.getSelectedStyle().setBorder(Border.createLineBorder(1, 0x4898d0));
        title.getPressedStyle().setBorder(Border.createLineBorder(1, 0x4898d0));
        
        Label spac = new Label(),spac1 = new Label();
        spac.setPreferredW(2);
        spac.setPreferredH(40);
        spac1.setPreferredW(2);
        spac1.setPreferredH(40);
        S.setTrans(spac);S.setTrans(spac1);
        
        addComponent(topcon);
        try {
            Image ima = Image.createImage("/edit.png");
            Button btnedit = new Button(ima);
            ima = Image.createImage("/add.png");
            Button btnnew = new Button(ima);
            S.setTrans(btnedit);S.setTrans(btnnew);
            btnedit.setPreferredW(50);
            btnnew.setPreferredW(50);
            btnedit.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    if(currTaxi!=null){
                    ed = new EditDialog(((Taxi)((Prov)taxr.prov.elementAt(idProv)).taxi.elementAt(lv.getSelectedIndex())).name,((Taxi)((Prov)taxr.prov.elementAt(idProv)).taxi.elementAt(lv.getSelectedIndex())).num);
                    ed.show();
                    }
                }
            });
            btnnew.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    if(currTaxi!=null){
                    ed = new EditDialog();
                    ed.show();
                    }
                }
            });
            spac.getStyle().setBgImage(Image.createImage("/spacer.png"));
            spac1.getStyle().setBgImage(Image.createImage("/spacer.png"));
            ima = Image.createImage("/top.png");
            topcon.getStyle().setBgImage(ima);
            topcon.addComponent(title);
            topcon.addComponent(spac);
            topcon.addComponent(btnedit);
            topcon.addComponent(spac1);
            topcon.addComponent(btnnew);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        title.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                if(pd==null)pd = new ProvDialog();
                pd.show();
            }
        });
        
        Container con = new Container(new BoxLayout(BoxLayout.X_AXIS));
        con.getStyle().setBgTransparency(255);
        try {
            con.getStyle().setBgImage(Image.createImage("/bot.png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        con.setPreferredW(getWidth());
        con.setPreferredH(49);
        
        try {
            bcall = new Button(Image.createImage("/phoneicon.png"));
            addressbook = new Button(Image.createImage("/addrbookactiv.png"));
            bookmark = new Button(Image.createImage("/bookmarkicon.png"));
            setting = new Button(Image.createImage("/setting.png"));
            con.addComponent(bcall);
            con.addComponent(addressbook);
            con.addComponent(bookmark);
            con.addComponent(setting);
            settran(bcall, Image.createImage("/phoneiconactiv.png"));
            settran(addressbook, Image.createImage("/addrbookactiv.png"));
            settran(bookmark, Image.createImage("/bookmarkiconactiv.png"));
            settran(setting, Image.createImage("/settingactiv.png"));
            bcall.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    Main.getInstance().showMain();
                }
            });
            bookmark.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    Main.getInstance().showBookmark();
                }
            });
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        addComponent(con);
    }
    EditDialog ed;
    void settran(Button b,Image i){
        Style s = new Style();
        s.setBgTransparency(0);
        s.setMargin(0,0,0,0);
        s.setPadding(0,0,0,0);
        s.setBorder(null);
        Style s1 = new Style();
        s1.setBgTransparency(0);
        s1.setMargin(0,0,0,0);
        s1.setPadding(0,0,0,0);
        s1.setBorder(Border.createLineBorder(1, 0xffffff));
        b.setUnselectedStyle(s);
        b.setSelectedStyle(s1);
        b.setPressedStyle(s);
        b.setPressedIcon(i);
    }
    int page = 0;Label searching;
    Image imfav;
    void addTx(Taxi t){
        Label lblnum,lblname;
        Container out = new Container(new BoxLayout(BoxLayout.X_AXIS));
        Container con = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        lblnum = new Label(t.num);
        lblname = new Label(t.name);
        lblnum.setEnabled(false);
        Label lbl = new Label();
        lbl.setEnabled(false);
        lbl.setPreferredW(getWidth()-40);
        lbl.setPreferredH(1);
        lbl.getDisabledStyle().setBgTransparency(255);
        lbl.getDisabledStyle().setBgColor(0xeeeeee);
        lblnum.getDisabledStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE));
        con.setPreferredW(getWidth()-60);
        con.setPreferredH(70);
        
        con.addComponent(lblnum);
        con.addComponent(lblname);
        con.addComponent(lbl);
        
        out.setPreferredH(70);
        out.setPreferredW(getWidth());
        if(imfav==null)try {
            imfav = Image.createImage("/fav.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        Label lbfav = new Label();
        lbfav.setEnabled(false);
        lbfav.getDisabledStyle().setBgImage(imfav);
        if(t.bookmark)lbfav.setVisible(true);
        else lbfav.setVisible(false);
        lbfav.setPreferredH(60);
        lbfav.setPreferredW(60);
        out.addComponent(con);
        out.addComponent(lbfav);
        t.lbfav = lbfav;
        lv.addItem(out);
    }
    public void show() {
        super.show();
        setFocused(addressbook);
    }
    void call(Taxi t){
        Main.getInstance().call(t.num);
        MainForm.setCall(t);
    }
    
    class callrecord{
        Taxi t;
        int idTaxi;
        String time;
    }
    
    void addTaxi(){
        try {
            Taxi t = new Taxi();
            System.out.println(""+(((Prov)taxr.prov.elementAt(idProv)).name)+":"+((Prov)taxr.prov.elementAt(idProv)).taxi.size());
            if(lv!=null&&contains(lv))removeComponent(lv);
            lv = new List();
            lv.setRenderer(new ListCellRender());
            lv.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    if(lv.getSelectedIndex()>=0){
                        if(m==null)m = new MenuDialog();
                        m.show();
                    }
                }
            });
            lv.addSelectionListener(new SelectionListener() {
                public void selectionChanged(int i, int i1) {
                    currTaxi = ((Taxi)((Prov)taxr.prov.elementAt(idProv)).taxi.elementAt(lv.getSelectedIndex()));
                }
            });
            lv.setRenderer(new ListCellRender());
            lv.setPreferredH(getHeight()-89);
            lv.setPreferredW(getWidth());
            addComponent(1,lv);
            for (int x = 0; x <= ((Prov)taxr.prov.elementAt(idProv)).taxi.size(); x++){
                try{
                    t = (Taxi)((Prov)taxr.prov.elementAt(idProv)).taxi.elementAt(x);
                    addTx(t);
                }catch(Exception ex){
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } 
    }
    public static byte[] Bytes(int value) {
        return new byte[] {
                (byte)(value >>> 24),
                (byte)(value >>> 16),
                (byte)(value >>> 8),
                (byte)value};
    }
    public static int Int(byte [] b) {
        return (b[0] << 24)
                + ((b[1] & 0xFF) << 16)
                + ((b[2] & 0xFF) << 8)
                + (b[3] & 0xFF);
    }
    Command com,exit;
    void addCmd(){
        try {
            com = new Command("Tùy chọn");
            exit = new Command("Thoát");
            removeAllCommands();
            addCommand(com);
            addCommand(exit);
            getSoftButton(0).setPreferredH(30);
            getSoftButton(1).setPreferredH(30);
            repaint();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    protected void actionCommand(Command cmd) {
        super.actionCommand(cmd);
        if(cmd.equals(com)){
            if(m==null) m = new MenuDialog();
            m.show(60,0,0,40,true,false);
        }else{
        }
    }
    
    Button bookmarkbot,callbot,about,help,cancel;
    MenuDialog m;
    int []favlist;
    Command back,emp;
    int contextFocus = 0;
    
    class MenuDialog extends Dialog{
        public MenuDialog() {
            setLayout(new BoxLayout(BoxLayout.Y_AXIS));
            getStyle().setAlignment(Component.CENTER);
            bookmark = new Button("Lưu/Bỏ lưu");
            callbot = new Button("Gọi");
            about = new Button("Thông tin");
            help = new Button("Giúp đỡ");
            cancel = new Button("Quay lại");
            S.setTrans(bookmark);S.setTrans(callbot);
            S.setTrans(about);S.setTrans(help);
            S.setTrans(cancel);
            try{
                back = new Command("Quay lại");
            }catch(Exception e){}
            addComponent(bookmark);
            addComponent(callbot);
            addComponent(about);
            addComponent(help);
            addComponent(cancel);
            addCommand(back);
            Style s = new Style();
            s.setMargin(0,0,0,0);
            s.setPadding(0,0,0,0);
            s.setBorder(null);
            getSoftButton(0).setUnselectedStyle(s);
            getSoftButton(0).setPressedStyle(s);
            getSoftButton(0).setSelectedStyle(s);
            getSoftButton(0).setFocusable(false);
            getSoftButton(1).setUnselectedStyle(s);
            getSoftButton(1).setPressedStyle(s);
            getSoftButton(1).setSelectedStyle(s);
            getSoftButton(1).setFocusable(false);
            bookmark.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    try {
                        Taxi cur = ((Taxi)((Prov)taxr.prov.elementAt(idProv)).taxi.elementAt(lv.getSelectedIndex()));
                        if(cur.bookmark){
                            cur.unBookmark();
                            cur.lbfav.setVisible(false);
                        }else{
                            cur.bookmark();
                            cur.lbfav.setVisible(true);
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    } 
                    m.dispose();
                }
            });
            callbot.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    m.dispose();
                    Taxi t = ((Taxi)((Prov)taxr.prov.elementAt(idProv)).taxi.elementAt(lv.getSelectedIndex()));
                    call(t);
                }
            });
            about.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    m.dispose();
                    Main.getInstance().showAbout();
                }
            });
            help.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    m.dispose();
                    Main.getInstance().showHelp();
                }
            });
            cancel.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    m.dispose();
                }
            });
        }
    }
    List l;
    ProvDialog pd;
    class ProvDialog extends Dialog{
        public ProvDialog() {
            setLayout(new BoxLayout(BoxLayout.Y_AXIS));
            getStyle().setAlignment(Component.CENTER);
            l = new List();
            Label lab;
            for(int i=0;i<province.length;i++){
                lab = new Label(province[i]);
                lab.setPreferredH(45);
                l.addItem(lab);
            }
            l.setRenderer(new ListCellRender());
            try{
                back = new Command("Quay lại");
            }catch(Exception e){}
            addComponent(l);
            addCommand(back);
            l.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    pd.dispose();
                    idProv = l.getSelectedIndex();
                    title.setText(((Prov)taxr.prov.elementAt(idProv)).name.substring(5));
                    addTaxi();
                }
            });
        }
    }
    class EditDialog extends Dialog{
        Label lbl1,lbl2;
        TextArea txt1,txt2;
        Button ok,cancel;
        public EditDialog() {
            setLayout(new BoxLayout(BoxLayout.Y_AXIS));
            getStyle().setAlignment(Component.CENTER);
            lbl1 = new Label("Tên hãng:");
            lbl2 = new Label("Số điện thoại:");
            txt1 = new TextArea();
            txt2 = new TextArea();
            Container con = new Container(new BoxLayout(BoxLayout.X_AXIS));
            ok = new Button("Lưu");
            cancel = new Button("Quay lại");
            con.addComponent(ok);
            con.addComponent(cancel);
            addComponent(lbl1);
            addComponent(txt1);
            addComponent(lbl2);
            addComponent(txt2);
            addComponent(con);
            ok.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    ed.dispose();
                    try {
                        RecordStore rs = RecordStore.openRecordStore("dataTaxi", true);
                        //id,name,num,provin
                        String s = ","+txt1.getText()+","+txt2.getText()+","+idProv;
                        rs.addRecord(s.getBytes(), 0, s.getBytes().length);
                        Taxi t = new Taxi();
                        t.name = txt1.getText();
                        t.num = txt2.getText();
                        ((Prov)taxr.prov.elementAt(idProv)).taxi.addElement(t);
                        addTx(t);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            });
            cancel.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    ed.dispose();
                }
            });
        }
        public EditDialog(String name,String num) {
            setLayout(new BoxLayout(BoxLayout.Y_AXIS));
            getStyle().setAlignment(Component.CENTER);
            lbl1 = new Label("Tên hãng:");
            lbl2 = new Label("Số điện thoại:");
            txt1 = new TextArea(name);
            txt2 = new TextArea(num);
            Container con = new Container(new BoxLayout(BoxLayout.X_AXIS));
            ok = new Button("Lưu");
            cancel = new Button("Quay lại");
            con.addComponent(ok);
            con.addComponent(cancel);
            addComponent(lbl1);
            addComponent(txt1);
            addComponent(lbl2);
            addComponent(txt2);
            addComponent(con);
            ok.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    ed.dispose();
                    try {
                        RecordStore rs = RecordStore.openRecordStore("dataTaxi", true);
                        //id,name,num,provin
                        String s = ""+currTaxi.ind+","+txt1.getText()+","+txt2.getText()+","+idProv;
                        rs.addRecord(s.getBytes(), 0, s.getBytes().length);
                        Taxi t = new Taxi();
                        t.name = txt1.getText();
                        t.num = txt2.getText();
                        ((Prov)taxr.prov.elementAt(idProv)).taxi.addElement(t);
                        addTx(t);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            });
            cancel.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    ed.dispose();
                }
            });
        }
    }
}
